﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using StartEAS_API.Utils;
using StartsEAS_API.BLL.Interfaces;
using StartsEAS_API.BLL.Interfaces.Student;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Data.OleDb;
using System.Diagnostics;

namespace StartsEAS_API.Controllers
{
    [ApiController]
    [Route("[controller]/[action]")]
    [EnableCors("any")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };
        private static string CmdPath = @"C:\Windows\System32\cmd.exe";
        private readonly ILogger<WeatherForecastController> _logger;
        private readonly ILoginBLL _loginBll;
        private readonly IStudentBLL _studentBll; 
        private readonly StarsEASContext Context;
        public WeatherForecastController(ILogger<WeatherForecastController> logger, StarsEASContext context, ILoginBLL loginBll, IStudentBLL studentBll)
        {
            _logger = logger;
            Context = context;
            _loginBll = loginBll;
            _studentBll = studentBll;
        }
        /// <summary>
        /// 默认生成接口
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }
        /// <summary>
        /// 学生登录接口 测试用
        /// </summary>
        /// <param name="username"></param>
        /// <param name="pwd"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils stuLogin(string username, string pwd,int state)
        {
            if (state == 1) return _loginBll.GetStudent(username, pwd);
            else return _loginBll.GetTeacher(username, pwd);
        }
        /// <summary>
        /// 教师登录接口 测试用
        /// </summary>
        /// <param name="username"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        [HttpPost] // Form-Data  x-www-form-urlencoded
        public ResultUtils GetTeacher([FromForm] string username, [FromForm] string pwd)
        {
            return _loginBll.GetTeacher(username, pwd);
        }

        //[HttpPost] // Params
        //public ResultUtils GetTeacher(string username,string pwd)
        //{
        //    return _loginBll.GetTeacher(username, pwd);
        //}

        //[HttpGet]
        //public string gj()
        //{
        //    string cmd = @"shutdown /s".Trim().TrimEnd('&') + "&exit";//说明：不管命令是否成功均执行exit命令，否则当调用ReadToEnd()方法时，会处于假死状态
        //    string output = "";
        //    using (Process p = new Process())
        //    {
        //        p.StartInfo.FileName = CmdPath;
        //        p.StartInfo.UseShellExecute = false;        //是否使用操作系统shell启动
        //        p.StartInfo.RedirectStandardInput = true;   //接受来自调用程序的输入信息
        //        p.StartInfo.RedirectStandardOutput = true;  //由调用程序获取输出信息
        //        p.StartInfo.RedirectStandardError = true;   //重定向标准错误输出
        //        p.StartInfo.CreateNoWindow = true;          //不显示程序窗口
        //        p.Start();//启动程序

        //        //向cmd窗口写入命令
        //        p.StandardInput.WriteLine(cmd);
        //        p.StandardInput.AutoFlush = true;

        //        //获取cmd窗口的输出信息
        //        output = p.StandardOutput.ReadToEnd();
        //        p.WaitForExit();//等待程序执行完退出进程
        //        p.Close();
        //    }
        //    return output;

        //}

    }
}
